package com.zueb.service.impl;

import com.zueb.mapper.CheckinMapper;
import com.zueb.pojo.Checkin;
import com.zueb.service.CheckinService;
import com.zueb.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class CheckinServiceImpl implements CheckinService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public int addCheckin(Checkin checkin) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        CheckinMapper checkinMapper = sqlSession.getMapper(CheckinMapper.class);
        //调用方法
        int add = checkinMapper.addCheckin(checkin);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        //返回值
        return add;
    }

    @Override
    public List<Checkin> showCheckin(String date, int facultyId, int classId, int curPage, int pageSize) {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        CheckinMapper checkinMapper = sqlSession.getMapper(CheckinMapper.class);
        //调用方法
        int index = pageSize * (curPage - 1);
        List<Checkin> showCheckin = checkinMapper.getCheckin(date, facultyId, classId, index, pageSize);
        //释放资源
        sqlSession.close();

        return showCheckin;
    }

    @Override
    public int showCheckinCount(String date, int facultyId, int classId) {
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        CheckinMapper checkinMapper = sqlSession.getMapper(CheckinMapper.class);
        //调用方法
        int count= checkinMapper.getCheckinCount(date, facultyId, classId);
        //释放资源
        sqlSession.close();

        return count;
    }

    @Override
    public List<Checkin> showCheckinByNumber(String userNumber, int curPage, int pageSize, String date) {
        //获取SQLSession
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        CheckinMapper checkinMapper = sqlSession.getMapper(CheckinMapper.class);
        //调用方法
        int index = pageSize * (curPage - 1);
        List<Checkin> checkinByNumber = checkinMapper.getCheckinByNumber(date, userNumber, index, pageSize);
        //释放资源
        sqlSession.close();
        //返回值
        return checkinByNumber;
    }

    @Override
    public int showCheckinByNumberCount(String userNumber, String date) {
        //获取SQLSession
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        CheckinMapper checkinMapper = sqlSession.getMapper(CheckinMapper.class);
        //调用方法
        int count= checkinMapper.getCheckinByNumberCount(date, userNumber);
        //释放资源
        sqlSession.close();
        return count;
    }


}
